/**
 * @author tmh
 * @date 2024/10/14 22:18
 * @description
 */
public class T59螺旋矩阵II {
    public int[][] generateMatrix(int n) {
        //定义四个变量来进行一个边界控制
        int left = 0;
        int right = n - 1;
        int top = 0;
        int bottom = n - 1;
        int target = n * n;
        int num = 1;
        int[][] result = new int[n][n];
        while (num <= target) {
            for (int i=left;i<=right;i++){
                result[top][i]=num++;
            }
            for (int i=++top;i<=bottom;i++){
                result[i][right]=num++;
            }
            for (int i =--right;i>=left;i--){
                result[bottom][i]=num++;
            }
            for (int i=--bottom;i>=top;i--){
                result[i][left]=num++;
            }
            left++;
        }
        return result;
    }
}
